#include <iostream>
#include <SFML/Graphics.hpp>
#include "../include/Configs.hpp"

using namespace std;

Configs::Configs()
{
    // Initialisation
    m_gui_config.hauteur_titre = 0;
    m_gui_config.padding = 0;
    m_gui_config.base_width = 0;
    m_gui_config.base_height = 0;
    m_gui_config.titre_size = 0;
    m_gui_config.contenu_size = 0;

    Configuration();
}

void Configs::Configuration()
{
    ConfigGame();
    ConfigGUI();
}

void Configs::ConfigGUI()
{
    TiXmlDocument doc("Config/GUI_config.xml");
    if(!doc.LoadFile()){
        cerr << "XML -> Erreur lors du chargement" << endl;
        cerr << "XML -> Error #" << doc.ErrorId() << " : " << doc.ErrorDesc() << endl;
    }

    TiXmlHandle hdl(&doc);
    TiXmlElement *elem = hdl.FirstChildElement("config").FirstChildElement("dimensions").Element();

    if(!elem)
    {
        cerr << "XML -> Le noeud à atteindre n'existe pas" << endl;
    }

    elem->QueryIntAttribute("hauteur_titre", &m_gui_config.hauteur_titre);
    elem->QueryIntAttribute("padding", &m_gui_config.padding);
    elem->QueryIntAttribute("base_width", &m_gui_config.base_width);
    elem->QueryIntAttribute("base_height", &m_gui_config.base_height);

    elem = elem->NextSiblingElement("font"); // iteration

    if(!m_gui_config.font_titre.LoadFromFile(elem->Attribute("titre")))
    {
        cout << "Erreur lors du chargement de la police de caractères du titre" << endl;
    }
    elem->QueryIntAttribute("titre_size", &m_gui_config.titre_size);

    if(!m_gui_config.font_contenu.LoadFromFile(elem->Attribute("contenu")))
    {
        cout << "Erreur lors du chargement de la police de caractères du contenu" << endl;
    }
    elem->QueryIntAttribute("contenu_size", &m_gui_config.contenu_size);

    elem = elem->NextSiblingElement("titre_color"); // iteration

    int r = 0;
    int g = 0;
    int b = 0;
    int a = 0;

    elem->QueryIntAttribute("r", &r);
    elem->QueryIntAttribute("g", &g);
    elem->QueryIntAttribute("b", &b);
    elem->QueryIntAttribute("a", &a);

    m_gui_config.color_titre = sf::Color(r,g,b,a);

    elem = elem->NextSiblingElement("contenu_color"); // iteration

    elem->QueryIntAttribute("r", &r);
    elem->QueryIntAttribute("g", &g);
    elem->QueryIntAttribute("b", &b);
    elem->QueryIntAttribute("a", &a);

    m_gui_config.color_contenu = sf::Color(r,g,b,a);
}
void Configs::ConfigGame()
{
    TiXmlDocument doc("Config/Game_config.xml");
    if(!doc.LoadFile()){
        cerr << "XML -> Erreur lors du chargement" << endl;
        cerr << "XML -> Error #" << doc.ErrorId() << " : " << doc.ErrorDesc() << endl;
    }

    TiXmlHandle hdl(&doc);
    TiXmlElement *elem = hdl.FirstChildElement("config").FirstChildElement().Element();

    if(!elem)
    {
        cerr << "XML -> Le noeud à atteindre n'existe pas" << endl;
    }

    elem->QueryIntAttribute("width", &m_game_config.screen_width);
    elem->QueryIntAttribute("height", &m_game_config.screen_height);
    elem->QueryIntAttribute("colors", &m_game_config.screen_colors);
    elem->QueryIntAttribute("resizable", &m_game_config.resizable);
    elem->QueryIntAttribute("closing", &m_game_config.closing);
	elem = elem->NextSiblingElement(); // iteration
    elem->QueryIntAttribute("rate", &m_game_config.frame_rate);
}

gui_config* Configs::GetGUI()
{
    return &m_gui_config;
}
game_config* Configs::GetGame()
{
    return &m_game_config;
}
